package net.bwie.realtime.traffic.service.trafficrealtimeservice.controller;

import com.alibaba.fastjson.JSON;
import net.bwie.realtime.traffic.service.trafficrealtimeservice.bean.SugarIllegalEvents;
import net.bwie.realtime.traffic.service.trafficrealtimeservice.bean.SugarOne;
import net.bwie.realtime.traffic.service.trafficrealtimeservice.bean.SugarTwo;
import net.bwie.realtime.traffic.service.trafficrealtimeservice.service.TrafficService;
import net.bwie.realtime.traffic.service.trafficrealtimeservice.utils.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/traffic")
public class TrafficController {
    @Autowired
    TrafficService trafficService;

    static final String defaultDate = "1970-01-01";

    @RequestMapping("/illegalEvents")
    public String getIllegalEvents(@RequestParam(value = "date",defaultValue = defaultDate)String date) {

        if ((defaultDate.equals(date))) {
            date = DateUtil.nowDate();
        }

        List<SugarIllegalEvents> illegalEventsList = trafficService.getIllegalEvents(date);

        if(null == illegalEventsList || illegalEventsList.isEmpty()){
            return "[]";
        }

        ArrayList<SugarOne> name = new ArrayList<>();
        name.add(new SugarOne("车牌号", "licensePlate"));
        name.add(new SugarOne("道路ID",  "roadId"));
        name.add(new SugarOne("车道号", "lane"));
        name.add(new SugarOne("事件类型",  "eventType"));

        return "{\n" +
                "  \"status\": 0,\n" +
                "  \"hitSugarSelf\": true,\n" +
                "  \"msg\": \"\",\n" +
                "  \"data\": {\n" +
                "    \"columns\": "+
                JSON.toJSONString(name)
                +",\n" +
                "    \"rows\": "+
                JSON.toJSONString(illegalEventsList)
                +"\n" +
                "  }\n" +
                "}";
    }

    @RequestMapping("/illegalType")
    public String getIllegalType(@RequestParam(value = "date",defaultValue = defaultDate)String date) {

        if ((defaultDate.equals(date))) {
            date = DateUtil.nowDate();
        }

        List<SugarTwo> illegalTypeList = trafficService.getIllegalType(date);

        if(null == illegalTypeList || illegalTypeList.isEmpty()){
            return "[]";
        }

        return "{\n" +
                "  \"status\": 0,\n" +
                "  \"hitSugarSelf\": true,\n" +
                "  \"msg\": \"\",\n" +
                "  \"data\": "+
                JSON.toJSONString(illegalTypeList)
                +"\n" +
                "}";
    }
}
